import { _decorator, Animation, Component, Label, macro, Node, Sprite, SpriteAtlas, tween, v3 } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('Task')
export class Task extends Component {
    
    @property(Sprite)
    pic: Sprite = null!

    @property(Node)
    bottom: Node = null!

    @property(Label)
    count: Label = null!

    @property(Node)
    check: Node = null!

    type: number = 1

    amount: number = 1

    scale: number = 1

    finished: boolean = false

    init(type: number, amount: number = 1) {
        this.type = type
        this.pic.spriteFrame = this.pic.spriteAtlas.getSpriteFrame(type + '')
        this.amount = amount
        this.count.string = '' + amount * 3
        this.changeFinished(false)
        this.playAnime()
    }

    rescale(scale: number) {
        this.scale = scale
        this.node.scale = v3(scale, scale, scale)
    }

    playAnime() {
        let i: number = 1.5
        this.schedule(() => {
            if (this.check.active) {
                return
            }
            let node: Node = this.node
            let scale: number = this.scale * 1.1
            tween(node)
                .to(i / 2, { scale: v3(scale, scale, scale) }, { easing: 'quadOut' })
                .to(i / 2, { scale: v3(this.scale, this.scale, this.scale) }, { easing: 'quadOut' })
                .start()
        }, i)
    }

    changeFinished(finished: boolean) {
        this.finished = finished
        this.bottom.active = !finished
        this.check.active = finished
    }
}


